package com.wh.wisdomsite.xbox.core.threadpool;

import java.util.concurrent.*;


/**
 * 线程池的创建
 * @Package com.wh.wisdomsite.xbox.core.threadpool
 * @author 谢泽鹏
 * @date 2014年10月8日 下午4:02:14
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2014
 * @version V1.0
 */
public class JobThreadPoolExecutor extends ThreadPoolExecutor {


    /**
     * 任务转发器
     */
    private JobDispathcher jobDispathcher;


    /**
     * 创建线程池
     * @param corePoolSize    核心线程数
     * @param maximumPoolSize 最大线程数
     * @param keepAliveTime   存活时间(当实际线程数大于核心线程数时，允许该时间内终止之前等待新的任务)
     * @param unit            时间单位
     * @param workQueue       任务队列
     */
    public JobThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    /**
     * 创建线程池
     * @param corePoolSize    核心线程数
     * @param maximumPoolSize 最大线程数
     * @param keepAliveTime   存活时间(当实际线程数大于核心线程数时，允许该时间内终止之前等待新的任务)
     * @param unit            时间单位
     * @param workQueue       任务队列
     * @param threadFactory   线程工厂
     * @param jobDispathcher
     */
    public JobThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, JobDispathcher jobDispathcher) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
        this.jobDispathcher = jobDispathcher;
    }

    /**
     * 创建线程池
     * @param corePoolSize    核心线程数
     * @param maximumPoolSize 最大线程数
     * @param keepAliveTime   存活时间(当实际线程数大于核心线程数时，允许该时间内终止之前等待新的任务)
     * @param unit            时间单位
     * @param workQueue       任务队列
     * @param handler         线程handler
     */
    public JobThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
    }


    /**
     * 创建线程池
     * @param corePoolSize    核心线程数
     * @param maximumPoolSize 最大线程数
     * @param keepAliveTime   存活时间(当实际线程数大于核心线程数时，允许该时间内终止之前等待新的任务)
     * @param unit            时间单位
     * @param workQueue       任务队列
     * @param threadFactory   线程工厂
     * @param handler         线程handler
     */
    public JobThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory,RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }


    /**
     * (重写)在指定线程池前执行
     * @param thread
     * @param runnable
     */
    @Override
    protected void beforeExecute(Thread thread, Runnable runnable) {
        if (thread instanceof Job) {
            jobDispathcher.beforeExecute((Job) runnable);
        }
    }

    /**
     * (重写)在指定线程池后执行
     * @param runnable
     * @param throwable
     */
    @Override
    protected void afterExecute(Runnable runnable, Throwable throwable) {
        if (runnable instanceof Job) {
            jobDispathcher.releaseResource((Job) throwable);
        }
    }
}
